package com.tianqingwl.gsystem.tags.processors;

import com.tianqingwl.gsystem.content.pojo.ContentInfo;
import com.tianqingwl.gsystem.content.service.ContentInfoService;
import com.tianqingwl.gsystem.resources.service.SysConfigService;
import com.tianqingwl.gsystem.tags.GSystemDialect;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import org.thymeleaf.context.ITemplateContext;
import org.thymeleaf.model.IAttribute;
import org.thymeleaf.model.IProcessableElementTag;
import org.thymeleaf.processor.element.AbstractElementTagProcessor;
import org.thymeleaf.processor.element.IElementTagStructureHandler;
import org.thymeleaf.templatemode.TemplateMode;

import java.util.List;
import java.util.Map;

/**
 * 文章内容标签
 */
@Component
public class ContentInfoTagProcessor extends AbstractElementTagProcessor {


    private static final String TAG_NAME="contentInfo";
    private static final int PRECEDENCE = 10000;

    @Autowired
    private ContentInfoService contentInfoService;

    public ContentInfoTagProcessor() {
        super(TemplateMode.HTML, GSystemDialect.PREFIX, TAG_NAME, true, null, false, PRECEDENCE);
    }

    @Override
    protected void doProcess(ITemplateContext context, IProcessableElementTag tag, IElementTagStructureHandler structureHandler) {
        IAttribute typeAttribute = tag.getAttribute("type");
        if (typeAttribute!=null){
            String type = typeAttribute.getValue();
            if (!StringUtils.isEmpty(type)){
                switch (type){
                    case "hotContent":
                        List<ContentInfo> contentInfos = contentInfoService.listHotContent();
                        structureHandler.setLocalVariable("hotContent",contentInfos);
                        break;
                }
            }
        }
        structureHandler.removeTags();
    }
}
